$(function() {
	var ary = new Array();
	var comment_id;
	var comment_name;
	var fid;
	var uid;
	var loading = $("#my-modal-loading");
	var loadingtext = $("#loading_text");
	var page = 1;
	var btn = $("#bottom_more");
	var goods = function(){};
	
	var swiper = new Swiper('.swiper-container-ad', {
        pagination: '.swiper-pagination',
        paginationClickable: true,
        loop: true
    });
	$("#swiperwrapper").html(""); // 先清空当前id中所有的html文本
	var image1;
	var image2;
	var image3;
	var query_id = localStorage.getItem("query_id");
	var goods_id = localStorage.getItem("goods_id");
	var cid = localStorage.getItem("cid");
	var urlPath = "http://yx.fxtourong.com/V1/Goods/getGoodsDetails" + "?store_id=" + query_id + "&goods_id=" + goods_id + "&cid=" + cid;
	var getCpmmentListUrl = "http://yx.fxtourong.com/V1/Goods/getGoodsComments" + "?store_id=" + query_id;
	var sendCpmmentListUrl = "http://yx.fxtourong.com/V1/Goods/commentsToGoods" + "?store_id=" + query_id;
	console.log("url:" + urlPath);
	loading.modal();
	$.ajax({
			type: "GET",
			url: urlPath,
			data: {
				
			},
			dataType: "json",
			success: function(data) {
				console.log(data);
				var code = data.code;
				console.log(code);
				if(code == "1") {
					$("#yuyin_title").text(data.data.name);
					$("#topicDetails").text(data.data.details);
					$("#test_listen5").append(" <audio controls='controls'>" + "<source src='" + data.data.audio + "'" + +" type='audio/mpeg'>" + "</audio>");
					if(data.data.cover == "" || data.data.cover == undefined || data.data.cover == null){
						$("#swiper-container").css("display", "none");
					} else {
						var str="";
					    for(var i =0;i<data.data.cover.length;i++){
					        str+="<div class='swiper-slide'>"+
					            "<a class='lunbotu1'>"+
					                "<img height='340' class='sw_img' src='"+data.data.cover[i]+"' alt=''>"+
					            "</a>"+
					            "<p class='swiper_P'>"+"hello world"+"</p>"+
					        "</div>";
					    }
					    
					    $('.swiper-wrapper').append(str);　　　　　　//添加完以后就可以调用swiper了 哦了
					        var mySwiper = new Swiper('.swiper-container', {
					        // direction: 'vertical',
					        loop: true,
					        autoplay : 6000,
					        // 如果需要分页器
					        pagination: '.swiper-pagination',
					    });
					}
					var goodstype = new goods();
					goodstype.getGoodStatus();
				} else {
					loading.modal('close');
					alert(data.message + "");
				}
			},
			error: function(e){
				loading.modal('close');
				alert("服务请求异常！");
			}
		});	
		
	function getCommentList(){
		$.ajax({
			type: "POST",
			url: getCpmmentListUrl,  //12  store_id
			data: {
				goods_id: goods_id,
				page: page
			},
			dataType: "json",
			success: function(data) {
				btn.css("display", "block");
				loading.modal('close');
				var code = data.code;
				console.log("评论列表code:" + data.code);
				if(code == "1") {
					jsonStr = data.data;
					for(var i = 0; i<jsonStr.length; i++){
						var industry = jsonStr[i];	
						ary.push(industry);
						var timeStr = industry.created_time;
						var commentDetails = industry.content;
						var fid = industry.fid;
						if(fid == "0"){
							comment_name = industry.nickname;
							$("#comment_Ul").append("<li value='" + (i + 10*(page - 1)) + "' class='topiclist' style='border-bottom:none;border-top:0px solid #DEDEDE;padding-top:0px;margin-top:0px;margin-bottom:0px;' onclick=''>" + 
							"<div class='usershead'> "+ 
								"<img id='imagehead2' class='am-circle' src='" + industry.avatar + "' width='40' height='40' style='margin: 0 10px 0 0;'/>" + 
							"</div>" +
							"<div class='userMessage'>" + 
								"<p class='usersname'>" + industry.nickname + "</p>" + 
								"<p class='thetime'>" + timeStr + "</p>" + 
							"</div>" + 
							"<p class='commentDetails' style='padding: 0 20px 20px 20px;margin: 10px auto 0 auto;'>" + commentDetails + "</p>" + 
							
						"</li>"
							
							);
						} else if(fid == "1") {
							$("#comment_Ul").append("<li value='" + (i + 10*(page - 1)) + "' class='topiclist' style='border-bottom:none;border-top:0px solid #DEDEDE;margin-left:30px;margin-top:-30px;' onclick=''>" + 
							"<p class='commentDetails' style='padding: 0 20px 20px 20px;margin: 10px auto 0 20px;'><span style='color:#009CDA'>" + industry.from_nickname + "</span>" + " 回复 " + "<span style='color:#009CDA'>" + industry.to_nickname + "</span>" + " : " +  commentDetails + "</p>" + 
						"</li>"
							
							);
					} else {
						if(industry.from_nickname !== null && industry.from_nickname !== "" && industry.from_nickname !== undefined){
							$("#comment_Ul").append("<li value='" + (i + 10*(page - 1)) + "' class='topiclist' style='border-bottom:none;border-top:0px solid #DEDEDE;margin-left:30px;margin-top:-30px;' onclick=''>" + 
							
							"<p class='commentDetails' style='padding: 0 20px 20px 20px;margin: 10px auto 0 20px;'><span style='color:#009CDA'>" + industry.from_nickname + "</span>" + " 回复 " + "<span style='color:#009CDA'>" + industry.to_nickname + "</span>" + " : " +  commentDetails + "</p>" + 
							
						"</li>");
						}
						
					}
						
					}
				} else {
					btn.css("display", "none");
					if(page == 1){
						$(".comment_list").css("display", "none");
					}
//					alert(data.message + "");
				}
			},
			error:function(e){
				btn.css("display", "none");
				loading.modal('close');
				alert("服务请求异常!")
			}
		});
	}	
	
	var huifu_detail;
	
	$("#huifu").click(function(){
		huifu_detail = $("#huifu_detail").val();
		console.log("回复评论:" + huifu_detail);
		loadingtext.text("发送回复...");
		loading.modal();
		var type = 0;
		if(fid == "0"){
			 type = 1;			
		} else {
			 type = 2;
		}
		$.ajax({
				type: "POST",
				url: huifuCpmmentListUrl,	//44
				data: {
					token : localStorage.getItem("token"),
					type: type,
					comment_id : comment_id,
					to_uid : uid,
					content : huifu_detail
				},
				dataType: "json",
				success: function(data) {
					loading.modal('close');
					var code = data.code;
					console.log(code);
					if(code == "1") {
						location.href = "dongtai_detail.html";
					} else {
						alert(data.message + "");
					}
				},
				error: function(e){
					loading.modal('close');
					alert("服务请求异常!");
				}
			});
	});
	
	$(document).on("click",".topiclist",function(){
		var m = $(this).val();
		fid = ary[m].fid;
		requestModel.modal();
		if(fid == "0"){
			uid = ary[m].uid;
			comment_name = ary[m].nickname;
			comment_id = ary[m].id;
			$("#reponse_person").text("回复: " + ary[m].nickname);
		} else {
			uid = ary[m].from_uid;
			comment_name = ary[m].from_nickname;
			comment_id = ary[m].comment_id;
			$("#reponse_person").text("回复: " + ary[m].from_nickname);
		}
		
	});
	
	var person_comment;
	$("#sendMsg").click(function() {
		person_comment = $("#person_comment").text();
		console.log("评论:" + person_comment);
		loadingtext.text("发送评论...");
		loading.modal();
		$.ajax({
			type: "POST",
			url: sendCpmmentListUrl,	//44
			data: {
				token : localStorage.getItem("token"),
				goods_id : goods_id,
				content : person_comment
			},
			dataType: "json",
			success: function(data) {
				loading.modal('close');
				var code = data.code;
				console.log(code);
				if(code == "1") {
					location.href = "voice_topic_details.html";
				} else {
					alert(data.message + "");
				}
			},
			error: function(e){
				loading.modal('close');
				alert("服务请求异常!");
			}
		});
	});
	
	$(document).on("click",".topiclist",function(){
		var m = $(this).val();
//		$("#sendMsg").text("回复 " + ary[m].nickname + " :");
		requestModel.modal();
		$("#reponse_person").text("回复 " + ary[m].nickname);
		comment_id = ary[m].id;
	});
	
	var getGoodNum = "http://yx.fxtourong.com/V1/Goods/laudToGoods" + "?store_id=" + query_id;
	$("#goodu").click(function(){	//点赞
		if(goodStatus == 1){
			new Toast({context:$('body'),message:'您已经点赞'}).show();
			return;
		}
		loading.modal();
		$.ajax({
				type: "POST",
				url: getGoodNum,  //46 
				data: {
					token : localStorage.getItem("token"),
					goods_id: goods_id				
				},
				dataType: "json",
				success: function(data) {
					loading.modal('close');
					console.log(data);
					var code = data.code;
					console.log(code);
					if(code == "1") {
						$("#goodu").attr("src", "../../public/yx/img/good2.svg");
//						location.href = "dongtai_detail.html";													
					} else {
						alert(data.message + "");
					}
				},
				error:function(e){
					loading.modal('close');
					alert("服务请求异常!")
				}
			});
	});
	
	var getGoodStatus = "http://yx.fxtourong.com/V1/Goods/getGoodsLaudStatus" + "?store_id=" + query_id;
	
	goods.prototype.getGoodStatus=function(name){
    	$.ajax({
				type: "POST",
				url: getGoodStatus,  //46 
				data: {
					token : localStorage.getItem("token"),
					goods_id: goods_id				
				},
				dataType: "json",
				success: function(data) {
					loading.modal('close');
					console.log(data);
					var code = data.code;
					console.log(code);
					if(code == "1") {
						$("#goodu").attr("src", "../../public/yx/img/good2.svg");
						goodStatus = 1;
					} else {
						$("#goodu").attr("src", "../../public/yx/img/good.svg");
						goodStatus = 0;
					}
					getCommentList();
				},
				error:function(e){
					loading.modal('close');
					alert("服务请求异常!")
				}
			});
	}
	
});